package com.geeke.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.sys.entity.Role;
import com.geeke.sys.service.RoleService;
import com.geeke.utils.ResultUtil;

/**
 * 角色表 Controller
 */
@RestController
@RequestMapping(value = "/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        Role entity = roleService.get(id);
        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<Role> result = roleService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<Role> result = roleService.listAll(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "save")
    @RequiresPermissions(value={"role:create","role:update"}, logical=Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody Role role) {
        String id = roleService.save(role).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @PostMapping(value = "delete")
    @RequiresPermissions("role:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody Role role) {
        int rows = roleService.delete(role);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("role:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<Role> roles) {
        List<String> ids = roleService.bulkInsert(roles);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("role:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<Role> roles) {
        List<String> ids = roleService.bulkUpdate(roles);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("role:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<Role> roles) {
        int rows = roleService.bulkDelete(roles);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }
} 